﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Linq;

using ProjectBase.Core;

using Survey.Entity.Services;
using Survey.Common;
using Survey.Common.Data.Services;
using Survey.Common.Business.Services;

namespace Survey.Business.Services
{
    /// <summary>
    ///   All Business checks for 'UserProfile' on CRUD operations
    ///   are managed by: UserProfileFacade
    /// </summary>
    public class UserProfileFacade : BaseFacade<UserProfile, int, UserProfileFilter, IUserProfileDao>, IUserProfileFacade
    {
        #region IBaseFacade<T> override - FACTORY METHOD
        /// <summary>
        /// Factory Method pattern.
        /// A central and only point for creating new entity of the type UserProfile.
        /// </summary>
        /// <returns>new entity 'UserProfile' transient</returns>
        public override UserProfile CreateNew()
        {
            var entity = new UserProfile();
            entity.IsAnonymous = false;
            entity.LastActivityDate = DateTime.Now;
            entity.LastUpdateDate = DateTime.Now;
            entity.SerializedSession = new Dictionary<string, object>();
            return entity;
        }
        public virtual UserProfile CreateNew(string userName, string applicationName, bool isAnonymous)
        {
            var entity = CreateNew();
            entity.UserName = userName;
            entity.ApplicationName = applicationName;
            entity.IsAnonymous = isAnonymous;
            return entity;
        }
        /// <summary>
        /// Needed for the BaseFacade checks on Update() and Delete().
        /// </summary>
        /// <returns></returns>
        public override UserProfileFilter CreateNewFilter()
        {
            var filter = new UserProfileFilter();
            // TODO preset filter
            return filter;
        }
        #endregion IBaseFacade<T> override - FACTORY METHOD

        #region IUserProfileFacade
        public virtual UserProfile GetOrCreateUserProfile(string userName, bool isAnonymous, string applicationName)
        {
            var template = CreateNew(userName, applicationName, isAnonymous);
            try
            {
                var entity = Dao.GetOrCreateUserProfile(template);
                if (entity.Is())
                {
                    return entity;
                }
            }
            catch (Exception ex)
            {
                this.PublishFatal("Cannot get userProfile from storage", ex);
            }
            return template;
        }
        #endregion IUserProfileFacade

        #region properties
        #endregion properties
    }
}
